// pages/index/index.js
const app = getApp();

Page({
  data: {
    userInfo: null,
    ads: [],
    showAd: false
  },

  onLoad() {
    this.initPage();
  },

  onShow() {
    this.loadUserInfo();
    this.loadAds();
  },

  onPullDownRefresh() {
    this.loadAds();
    wx.stopPullDownRefresh();
  },

  // 初始化页面
  initPage() {
    this.loadUserInfo();
    this.loadAds();
  },

  // 加载用户信息
  loadUserInfo() {
    const userInfo = app.globalData.userInfo;
    if (userInfo) {
      this.setData({
        userInfo: userInfo
      });
    }
  },

  // 加载广告
  loadAds() {
    // 检查是否显示广告
    const shouldShowAd = app.shouldShowAd();
    
    if (shouldShowAd) {
      // 模拟广告数据
      const mockAds = [
        {
          id: 'ad_1',
          title: '银行房贷优惠',
          description: '低利率房贷，快速审批',
          imageUrl: 'https://via.placeholder.com/300x200',
          clickUrl: '/pages/bank-loan/bank-loan'
        },
        {
          id: 'ad_2',
          title: '房产中介服务',
          description: '专业房产咨询，一对一服务',
          imageUrl: 'https://via.placeholder.com/300x200',
          clickUrl: '/pages/real-estate/real-estate'
        }
      ];
      
      this.setData({
        ads: mockAds,
        showAd: true
      });
      
      // 更新用户广告浏览统计
      if (this.data.userInfo) {
        this.updateAdView();
      }
    } else {
      this.setData({
        showAd: false
      });
    }
  },

  // 更新广告浏览统计
  updateAdView() {
    const userInfo = this.data.userInfo;
    userInfo.statistics.totalAdViews += 1;
    
    // 保存到本地存储
    wx.setStorageSync('userInfo', userInfo);
    app.globalData.userInfo = userInfo;
    
    this.setData({
      userInfo: userInfo
    });
  },

  // 跳转到计算器
  goToCalculator() {
    wx.switchTab({
      url: '/pages/calculator/calculator'
    });
  },

  // 跳转到提前还款
  goToEarlyRepayment() {
    wx.navigateTo({
      url: '/pages/calculator/calculator?type=early'
    });
  },

  // 跳转到历史记录
  goToHistory() {
    wx.switchTab({
      url: '/pages/history/history'
    });
  },

  // 跳转到VIP页面
  goToVip() {
    wx.navigateTo({
      url: '/pages/vip/vip'
    });
  },

  // 跳转到关于页面
  goToAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },

  // 分享应用
  shareApp() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  // 广告点击
  onAdClick(e) {
    const ad = e.currentTarget.dataset.ad;
    
    // 更新用户广告点击统计
    if (this.data.userInfo) {
      const userInfo = this.data.userInfo;
      userInfo.statistics.totalAdClicks += 1;
      
      // 保存到本地存储
      wx.setStorageSync('userInfo', userInfo);
      app.globalData.userInfo = userInfo;
      
      this.setData({
        userInfo: userInfo
      });
    }
    
    // 跳转到广告链接
    if (ad.clickUrl) {
      wx.navigateTo({
        url: ad.clickUrl
      });
    }
  },

  // 分享给朋友
  onShareAppMessage() {
    return {
      title: '专业房贷计算器 - 精准计算，智能分析',
      path: '/pages/index/index',
      imageUrl: '/images/share.jpg'
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '专业房贷计算器 - 精准计算，智能分析',
      imageUrl: '/images/share.jpg'
    };
  }
}); 